package com.kma.summer2012.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kma.summer2012.client.services.session.CrumbsSessionService;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsToolItem;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class CrumbsSessionServiceImpl extends RemoteServiceServlet implements CrumbsSessionService {
    @Override
    public void setParameterWorkItem(String nameProperty, UICrumbsWorkItem property) {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute(nameProperty, property);
    }

    @Override
    public void setParameterTool(String nameProperty, UICrumbsToolItem property) {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute(nameProperty, property);
    }

    @Override
    public void setParameterRequest(String nameProperty, UICrumbsRequest property) {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute(nameProperty, property);
    }

    @Override
    public void setParameterInteger(String nameProperty, int property) {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute(nameProperty, property);
    }

    @Override
    public UICrumbsWorkItem getParameterWorkItem(String nameParameter) {
        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(nameParameter) == null)
            return null;
        // session and userid is available, looks like user is logged in.
        return (UICrumbsWorkItem) session.getAttribute(nameParameter);
    }

    @Override
    public UICrumbsRequest getParameterRequest(String nameParameter) {
        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(nameParameter) == null)
            return null;
        // session and userid is available, looks like user is logged in.
        return (UICrumbsRequest) session.getAttribute(nameParameter);
    }

    @Override
    public UICrumbsToolItem getParameterToolItem(String nameParameter) {
        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(nameParameter) == null)
            return null;
        // session and userid is available, looks like user is logged in.
        return (UICrumbsToolItem) session.getAttribute(nameParameter);
    }

    @Override
    public int getParameterInteger(String nameParameter) {
        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(nameParameter) == null)
            return 0;
        // session and userid is available, looks like user is logged in.
        return (Integer) session.getAttribute(nameParameter);
    }
}